<template>
    <el-dialog title='编辑' :visible.sync="formVisible">  
      <el-form :rules="rules" ref="dataForm" :model="editData" label-position="left" label-width="70px" style=''>           
       <el-form-item :label="$t('table.user.id')" prop="id">
          <el-input v-model="editData.id"></el-input>
       </el-form-item>           
       <el-form-item :label="$t('table.user.username')" prop="username">
          <el-input v-model="editData.username"></el-input>
       </el-form-item>           
       <el-form-item :label="$t('table.user.password')" prop="password">
          <el-input v-model="editData.password"></el-input>
       </el-form-item>
       <el-form-item :label="$t('table.user.lastLoginTime')" prop="lastLoginTime">
          <el-date-picker v-model="editData.lastLoginTime" type="datetime"  value-format="timestamp" placeholder="Please pick lastLoginTime">
          </el-date-picker>
       </el-form-item> 
       <el-form-item :label="$t('table.user.isLock')"><el-select class="filter-item" v-model="editData.isLock" placeholder="Please select isLock">           
           <el-option v-for="item in  isLockMaps" :key="item.value" :label="item.name" :value="item.value">
           </el-option>           
         </el-select>
       </el-form-item>           
       <el-form-item :label="$t('table.user.nickname')" prop="nickname">
          <el-input v-model="editData.nickname"></el-input>
       </el-form-item>  
      <el-form-item :label="$t('table.user.headimg')" prop="headimg">
           <Upload v-model="editData.headimg"></Upload>  
      </el-form-item>           
       <el-form-item :label="$t('table.user.userTypeId')" prop="userTypeId">
          <el-input v-model="editData.userTypeId"></el-input>
       </el-form-item>           
       <el-form-item :label="$t('table.user.wxToken')" prop="wxToken">
          <el-input v-model="editData.wxToken"></el-input>
       </el-form-item></el-form>  
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{$t('table.cancel')}}</el-button>       
        <el-button type="primary" @click="update">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
</template>
<script>
import { userPatch } from '@/api/user'
import Upload from '@/components/Upload/singleImage2'
const isLockMaps = [{ value: 1, name: '是' }, { value: 0, name: '否' }]

export default {
  name: 'userEditDialog',
  props: ['Visible', 'editData'],
  components: { Upload },
  data() {
    return {
      isLockMaps,
      formVisible: this.Visible,
      rules: {
        id: [{ required: true, message: 'id is required', trigger: 'change' }],
        username: [{ required: true, message: 'username is required', trigger: 'change' }],
        password: [{ required: true, message: 'password is required', trigger: 'change' }],
        lastLoginTime: [{ required: true, message: 'lastLoginTime is required', trigger: 'change' }],
        isLock: [{ required: true, message: 'isLock is required', trigger: 'change' }],
        nickname: [{ required: true, message: 'nickname is required', trigger: 'change' }],
        headimg: [{ required: true, message: 'headimg is required', trigger: 'change' }],
        userTypeId: [{ required: true, message: 'userTypeId is required', trigger: 'change' }],
        wxToken: [{ required: true, message: 'wxToken is required', trigger: 'change' }]
      }
    }
  },
  methods: {
    update() {
      console.log(this.editData)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.editData)
          delete tempData.lastLoginTime
          userPatch(tempData).then(() => {
            // for (const v of this.list) {
            //   if (v.id === this.temp.id) {
            //     const index = this.list.indexOf(v)
            //     this.list.splice(index, 1, this.temp)
            //     break
            //   }
            // }
            this.$emit('hiddenEditDialog')
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    cancel() {
      this.$emit('hiddenEditDialog')
    }
  },
  watch: {
    Visible(val) {
      this.formVisible = val
    },
    formVisible(val) {
      if (val === false) {
        this.$emit('hiddenEditDialog')
      }
    }
  }
}
</script>

